/*
 * Copyright (c) 2012 Skyon Technology Ltd.
 * All rights reserved.
 *
 * project: sagittarius
 * create: 2012-9-19 下午02:49:17
 * cvs: $Id: $
 */
package cn.batch.daemon.bean;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;


/**
 * xml操作类
 * @author Administrator 
 * @version $Revision:$
 */
public class PreparApprovalxmlTool {
	private static Log logger = LogFactory.getLog(PreparApprovalxmlTool.class);

	/**将客户信息组装成blaze规则库所需的xml格式.
	 * @throws IOException */
	public static String writePreParApprovalXml(IN_PreApproval inVal) throws IOException
	{
		/* 建立document对象 */
		Document document = DocumentHelper.createDocument();
		/*建立根节点 */
		Element sqrElement = document.addElement("APPLICANTION_INFO");
		
		/*创建APPLICANT_INFO节点(申请人信息)*/
		Element APPLICANT_INFO = sqrElement.addElement("APPLICANT_INFO");
		if(inVal.getApplicantInfo() != null)
		{
			ApplicantInfo a = inVal.getApplicantInfo();
			//调用applicantInfoXml
			applicantInfoXml(APPLICANT_INFO,a);
			
		}
		
		
		/*创建APPLICANT_LOAN_INFO子节点*/
		Element APPLICANT_LOAN_INFO = sqrElement.addElement("APPLICANT_LOAN_INFO");
		if(inVal.getApplicantLoanInfo() != null)
		{
			ApplicantLoanInfo a = inVal.getApplicantLoanInfo();
			applicantLoanInfoXml(APPLICANT_LOAN_INFO,a);
			
		}
		/*创建APPLICANT_MORTGAGE_INFO节点*/
		Element APPLICANT_MORTGAGE_INFO = sqrElement.addElement("APPLICANT_MORTGAGE_INFO");
		if(inVal.getApplicantMortgageInfo() != null)
		{
			ApplicantMortgageInfo a = inVal.getApplicantMortgageInfo();
			applicantMortgageInfoXml(APPLICANT_MORTGAGE_INFO, a);
		}
		
		
		/*创建GUARANTOR_INFO节点*/
		Element GUARANTOR_INFO = sqrElement.addElement("GUARANTOR_INFO");
		if(inVal.getGuarantorInfo() != null)
		{
			GuarantorInfo g = inVal.getGuarantorInfo();
			guarantorInfoXml(GUARANTOR_INFO, g);
		}
		
		/**创建APPLICANT_SPOUSE_INFO节点*/
		Element APPLICANT_SPOUSE_INFO = sqrElement.addElement("APPLICANT_SPOUSE_INFO");
		if(inVal.getApplicantSpouseInfo() != null)
		{
			ApplicantSpouseInfo a = inVal.getApplicantSpouseInfo();
			applicantSpouseInfoXml(APPLICANT_SPOUSE_INFO, a);
		}
		
		
		
		/**创建APPLICANTION_DECISION_RESULT节点*/
		@SuppressWarnings("unused")
		Element APPLICANTION_DECISION_RESULT = sqrElement.addElement("APPLICANTION_DECISION_RESULT");
		
		
		/**创建APPLICANT_PBOC_INFO节点*/
		Element APPLICANT_PBOC_INFO = sqrElement.addElement("APPLICANT_PBOC_INFO");
		if(inVal.getApplicantPbocInfo() != null){
			ApplicantPbocInfo a = inVal.getApplicantPbocInfo();
			applicantPbocInfoXml(APPLICANT_PBOC_INFO,a);
		}
		/**创建APPLICANT_CM_INFO节点*/
		Element APPLICANT_CM_INFO = sqrElement.addElement("APPLICANT_CM_INFO");
		if(inVal.getApplicantCmInfo() != null)
		{
			ApplicantCmInfo a = inVal.getApplicantCmInfo();
			applicantCmInfoXml(APPLICANT_CM_INFO, a);
		}
		/**创建TRIGGER_RULE节点*/
		@SuppressWarnings("unused")
		Element TRIGGER_RULE = sqrElement.addElement("TRIGGER_RULE");
		
		/**xml文件转换成String*/
		String xmlString = doc2String(document);
		return xmlString;
	}


	/**
	 * @param GUARANTOR_INFO
	 * @param g
	 */
	private static void guarantorInfoXml(Element GUARANTOR_INFO, GuarantorInfo g) {
		//担保人本行贷款的最大逾期天数
		if(g.getGrarantorLoanOt() != null && !g.getGrarantorLoanOt().toString().trim().equals("")){
			GUARANTOR_INFO.addAttribute("GRARANTOR_LOAN_OT", g.getGrarantorLoanOt().toString());
		}
		//担保人是否行内黑名单客户
		if(g.getGuaBankBcFlag() != null && !g.getGuaBankBcFlag().trim().equals("")){
			GUARANTOR_INFO.addAttribute("GUA_BANK_BC_FLAG",g.getGuaBankBcFlag());
		}
		//担保人是否银监会黑名单客户
		if(g.getGuaCbrcBcFlag() != null && !g.getGuaCbrcBcFlag().trim().equals("")){
			GUARANTOR_INFO.addAttribute("GUA_CBRC_BC_FLAG",g.getGuaCbrcBcFlag());
		}
	}


	/**
	 * @param APPLICANT_SPOUSE_INFO
	 * @param a
	 */
	private static void applicantSpouseInfoXml(Element APPLICANT_SPOUSE_INFO, ApplicantSpouseInfo a) {
		//配偶本行贷款的最大逾期天数
		if(a.getSpoLoanOt() != null && !a.getSpoLoanOt().toString().trim().equals("")){
			APPLICANT_SPOUSE_INFO.addAttribute("SPO_LOAN_OT",a.getSpoLoanOt().toString() );
		}
		//申请人配偶是否行内黑名单客户
		if(a.getSpoBankBcFlag() != null && !a.getSpoBankBcFlag().trim().equals("")){
			APPLICANT_SPOUSE_INFO.addAttribute("SPO_BANK_BC_FLAG",a.getSpoBankBcFlag());
		}
		//申请人配偶是否银监会黑名单客户
		if(a.getSpoCbrcBcFlag() != null && !a.getSpoCbrcBcFlag().trim().equals("")){
			APPLICANT_SPOUSE_INFO.addAttribute("SPO_CBRC_BC_FLAG",a.getSpoCbrcBcFlag());
		}
		//申请人配偶同产品授信总额
		if(a.getSpoTotalCa() != null && !a.getSpoTotalCa().toString().trim().equals("")){
			APPLICANT_SPOUSE_INFO.addAttribute("SPO_TOTAL_CA",a.getSpoTotalCa().toString());
		}
		//申请人配偶行内贷款余额
		if(a.getSpoLoanBalance() != null && !a.getSpoLoanBalance().toString().trim().equals("")){
			APPLICANT_SPOUSE_INFO.addAttribute("SPO_LOAN_BALANCE",a.getSpoLoanBalance().toString());
		}
	}


	/**
	 * 组装APPLICANT_CM_INFO xml
	 * @param APPLICANT_CM_INFO
	 * @param a
	 */
	private static void applicantCmInfoXml(Element APPLICANT_CM_INFO, ApplicantCmInfo a) {
		//同一客户经理下同月处于逾期状态账户比率
		if(a.getOverdueAccountRateM1() != null){
			APPLICANT_CM_INFO.addAttribute("OVERDUE_ACCOUNT_RATE_M1", a.getOverdueAccountRateM1().toString());
		}
		//同一客户经理下同月逾期笔数
		if(a.getOverdueCountM1() != null){
			APPLICANT_CM_INFO.addAttribute("OVERDUE_COUNT_M1", a.getOverdueCountM1().toString());
		}
		//同一客户经理下过去6个月逾期客户
		if(a.getOverdueCustomerRateM6() != null){
			APPLICANT_CM_INFO.addAttribute("OVERDUE_CUSTOMER_RATE_M6", a.getOverdueCustomerRateM6().toString());
		}
		//同一客户经理下过去6个月逾期笔数
		if(a.getOverdueCountM6() != null){
			APPLICANT_CM_INFO.addAttribute("OVERDUE_COUNT_M6", a.getOverdueCountM6().toString());
		}
	}
	
	
	/**读取xml文件存入对象
	 * @throws DocumentException */
//	public static IN_PreApproval readPreApprovalXml(String xmlVal) throws DocumentException
//	{
//		Document doc = string2Document(xmlVal);
//		Element root = doc.getRootElement();
//		Element root2 = root.element("APPLICANTION_DECISION_RESULT");
//		@SuppressWarnings("unchecked")
//		List<Element> root3 = root2.elements("TRIGGER_RULE");
//		@SuppressWarnings("unchecked")
//		List<Element> root4 = root2.elements("DECISION_FLOW_ITEM_INFO");
//		//如果没有触发风险预警,既无决策结果,则返回Null
//		if(root2.attributes().size()<=0){
//			return null;
//		}
//		OUT_PreparApproval preparApproval = new OUT_PreparApproval();
//		//评分分数
//		if(root2.attribute("SCORE")!= null)
//		{
//			preparApproval.setScore(root2.attribute("SCORE").getValue());	
//		}
//		//业务模块
//		if(root2.attribute("BUSINESS_MODULE")!= null) 
//		{
//			preparApproval.setBusinessModule(root2.attribute("BUSINESS_MODULE").getValue());
//		}
//		//决策结果
//		if(root2.attribute("DECISION_RESULT")!= null)
//		{
//			preparApproval.setDecisionResult(root2.attribute("DECISION_RESULT").getValue());
//		}
//		//风险等级
//		if(root2.attribute("RISK_GRADE")!= null)
//		{
//			preparApproval.setRiskGrade(root2.attribute("RISK_GRADE").getValue());
//		}
//		//初始额度
//		if(root2.attribute("INITIAL_AMOUNT")!= null)
//		{
//			preparApproval.setInitialAmount(root2.attribute("INITIAL_AMOUNT").getValue());
//		}
//		//申请评分分数段系数
//		if(root2.attribute("SCORE_GRADE_RATE")!= null)
//		{
//			preparApproval.setScoreGradeRate(root2.attribute("SCORE_GRADE_RATE").getValue());
//		}
//		//最终贷款额度
//		if(root2.attribute("FINAL_LOAN_AMOUNT")!= null)
//		{
//			preparApproval.setFinalLoanAmount(root2.attribute("FINAL_LOAN_AMOUNT").getValue());
//		}
//		/**读取TRIGGER_RULE节点数据(节点数量不为一) */
//		List<OUTTriggerRuleApproval> list = new ArrayList<OUTTriggerRuleApproval>();
//		if(root3.size() > 0)
//		{
//			for(int i = 0;i < root3.size();i++)
//			{
//				Element e = root3.get(i);
//				OUTTriggerRuleApproval p = new OUTTriggerRuleApproval();
//				
//				if(e.attribute("RULESET_CODE") != null)
//				{
//					p.setRulesetCode(e.attribute("RULESET_CODE").getValue());
//				}
//				if(e.attribute("RULESET_NAME") != null)
//				{
//					p.setRulesetName(e.attribute("RULESET_NAME").getValue());
//				}
//				if(e.attribute("RULE_CODE") != null)
//				{
//					p.setRuleCode(e.attribute("RULE_CODE").getValue());
//				}
//				if(e.attribute("RULE_NAME") != null)
//				{
//					p.setRuleName(e.attribute("RULE_NAME").getValue());
//				}
//				if(e.attribute("RULE_PRIORITY") != null)
//				{
//					p.setRulePriority(e.attribute("RULE_PRIORITY").getValue());
//				}
//				if(e.attribute("RULE_RESULT_DESC") != null)
//				{
//					p.setRuleResultDesc(e.attribute("RULE_RESULT_DESC").getValue());
//				}
//				if(e.attribute("RULE_RESULT") != null)
//				{
//					p.setRuleResult(e.attribute("RULE_RESULT").getValue());
//				}
//				if(e.attribute("RULE_RISK_GRADE") != null)
//				{
//					p.setRuleRiskGrade(e.attribute("RULE_RISK_GRADE").getValue());
//				}
//				
//				@SuppressWarnings("unchecked")
//				List<Element> rootReason = root3.get(i).elements("TRIGGER_RULE");
//				List<Reason> listReason = new ArrayList<Reason>();
//				for(int y = 0;y < rootReason.size();i++){
//					Element eReason = rootReason.get(y);
//					Reason reason = new Reason();
//					if(eReason.attribute("REASON_CODE") != null){
//						reason.setReasonCode(eReason.attribute("REASON_CODE").getValue());
//					}
//					if(eReason.attribute("REASON_DETAIL") != null){
//						reason.setReasonDetail(eReason.attribute("REASON_DETAIL").getValue());
//					}
//					if(eReason.attribute("REASON_TYPE") != null){
//						reason.setReasonType(eReason.attribute("REASON_TYPE").getValue());
//					}
//					listReason.add(reason);
//				}
//				p.setListReason(listReason);
//				list.add(p);
//			}
//			preparApproval.setTriggerRuleApproval(list);
//		}
//		
//		List<DecisionFlowItemInfo> listdfi = new ArrayList<DecisionFlowItemInfo>();
//		if(root4.size() > 0){
//			
//			for(int i = 0;i < root4.size();i++){
//				DecisionFlowItemInfo decFlow = new DecisionFlowItemInfo();	
//				Element e = root4.get(i);
//				if(e.attribute("RULE_FLOW_NAME") != null){
//					decFlow.setRuleFlowName(e.attribute("RULE_FLOW_NAME").getValue());
//				}
//				if(e.attribute("RULE_FLOW_NUMBER") != null){
//					BigDecimal b= new BigDecimal(e.attribute("RULE_FLOW_NUMBER").getValue());
//					decFlow.setRuleFlowNumber(b);
//				}
//				
//				//List<DecisionComponentItemInfo> listDci = new ArrayList<DecisionComponentItemInfo>();
//				Set<DecisionComponentItemInfo> listDci = new HashSet<DecisionComponentItemInfo>();
//				for(int temp = 0;temp< e.elements("DECISION_COMPONENT_ITEM_INFO").size();temp++){
//					Element eComponent = (Element)e.elements("DECISION_COMPONENT_ITEM_INFO").get(temp);
//					DecisionComponentItemInfo dci = new DecisionComponentItemInfo();
//					if(eComponent.attribute("COMPONENT_NAME") != null){
//						dci.setComponentName(eComponent.attribute("COMPONENT_NAME").getValue());
//					}
//					if(eComponent.attribute("COMPONENT_NUMBER") != null){
//						BigDecimal b = new BigDecimal(eComponent.attribute("COMPONENT_NUMBER").getValue());
//						dci.setComponentNumber(b);
//					}
//					if(eComponent.attribute("COMPONENT_TYPE") != null){
//						dci.setComponentType(eComponent.attribute("COMPONENT_TYPE").getValue());
//					}
//					listDci.add(dci);
//					Set<RuleItemInfo> setRules = new HashSet<RuleItemInfo>(); 
//					//List<RuleItemInfo> listRule = new ArrayList<RuleItemInfo>();
//					for(int intRule = 0;intRule<eComponent.elements("RULE_ITEM_INFO").size();intRule++){
//						Element eRuleiteminfo =  (Element)eComponent.elements("RULE_ITEM_INFO").get(intRule);
//						RuleItemInfo ruleItemInfo = new RuleItemInfo();
//						if(eRuleiteminfo.attribute("RULE_NAME") != null){
//							ruleItemInfo.setRuleName(eRuleiteminfo.attribute("RULE_NAME").getValue());
//						}
//						if(eRuleiteminfo.attribute("RULE_NO") != null){
//							BigDecimal b = new BigDecimal(eRuleiteminfo.attribute("RULE_NO").getValue());
//							ruleItemInfo.setRuleNo(b);
//						}
//						setRules.add(ruleItemInfo);
//					}
//					dci.setRuleItemInfos(setRules);
//				}
//				decFlow.setDecisionComponentItemInfos(listDci);
//				listdfi.add(decFlow);
//			} 
//		}
//		preparApproval.setListDecFlowinfo(listdfi);
//		IN_PreApproval in = new IN_PreApproval();
//		in.setoUT_PreparApproval(preparApproval);
//		return in;
//	}
	
	
	/** 将xml文档内容转为String*/
	private static String doc2String(Document document)throws IOException {
		String s = "";
		try {
			// 使用输出流来进行转化
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			// 使用GB2312编码
			OutputFormat format = new OutputFormat("  ", true, "UTF-8");
			XMLWriter writer = new XMLWriter(out, format);
			writer.write(document);
			s = out.toString("UTF-8");
		} catch (IOException ex) {
			ex.printStackTrace();
			logger.error("风险预警客户信息的xml文档内容转为String时出错", ex);
			throw ex;
		}
		return s;
	}
	

	 /**将返回的字符串转换成xml类型
	 * @throws DocumentException */
	private static Document string2Document(String s) throws DocumentException{
		Document doc = null;
		try {
			doc = DocumentHelper.parseText(s);
		} catch (DocumentException ex) {
			ex.printStackTrace();
			logger.error("规则库处理结果转换为xml时出错", ex);
			throw ex;
		}
		return doc;
	}
	
	//组装APPLICANT_LOAN_INFO XMl文件
	private static void applicantLoanInfoXml(Element APPLICANT_LOAN_INFO,ApplicantLoanInfo a)
	{
		//是否有信易贷
		if(a.getCreditLoanFlag() != null && ! a.getCreditLoanFlag().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("CREDIT_LOAN_FLAG", a.getCreditLoanFlag());
		}
		//信易贷的贷款余额
		if(a.getClBanlance() != null && ! a.getClBanlance().toString().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("CL_BANLANCE",a.getClBanlance().toString());
		}	
		//是否有房易贷
		if(a.getHouseLoanFlag() != null && ! a.getHouseLoanFlag().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("HOUSE_LOAN_FLAG",a.getHouseLoanFlag() );
		}	
		//房易贷的贷款余额
		if(a.getHlBanlance() != null && ! a.getHlBanlance().toString().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("HL_BANLANCE",a.getHlBanlance().toString() );
		}	
		//是否有综合消费贷款
//		if(a.getConsumerLoanFlag() != null && ! a.getConsumerLoanFlag().trim().equals(""))
//		{
//			APPLICANT_LOAN_INFO.addAttribute("CONSUMER_LOAN_FLAG",a.getConsumerLoanFlag() );
//		}	
		//综合消费贷款余额
		if(a.getCclBanlance() != null && ! a.getCclBanlance().toString().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("CCL_BANLANCE",a.getCclBanlance().toString());
		}	
		//是否有担保人
//		if(a.getGuarantorFlag() != null && ! a.getGuarantorFlag().trim().equals(""))
//		{
//			APPLICANT_LOAN_INFO.addAttribute("GUARANTOR_FLAG",a.getGuarantorFlag() );
//		}	
		//授信额度是否在有效期
		if(a.getCaValidFlag() != null && ! a.getCaValidFlag().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("CA_VALID_FLAG",a.getCaValidFlag() );
		}	
		//贷款申请额度
		if( a.getLoanAmount()!= null && ! a.getLoanAmount().toString().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("LOAN_AMOUNT",a.getLoanAmount().toString());
		}	
		//贷款品种
		if(a.getLoanCategory() != null && ! a.getLoanCategory().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("LOAN_CATEGORY",a.getLoanCategory());
		}	
		//贷款账号
		if(a.getLoanAccount() != null && ! a.getLoanAccount().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("LOAN_ACCOUNT",a.getLoanAccount() );
		}	
		//抵质押率
		if(a.getMortgageRate() != null && ! a.getMortgageRate().toString().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("MORTGAGE_RATE",a.getMortgageRate().toString() );
		}	
		//贷款期限
		if(a.getLoanTerm() != null && ! a.getLoanTerm().toString().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("LOAN_TERM",a.getLoanTerm().toString());
		}	
		//借款用途
		if(a.getLoanUseage() != null && ! a.getLoanUseage().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("LOAN_USEAGE",a.getLoanUseage() );
		}	
		//贷款月支出
		if(a.getExpendiuteMonthly() != null && ! a.getExpendiuteMonthly().toString().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("EXPENDIUTE_MONTHLY",a.getExpendiuteMonthly().toString());
		}	
		//申请人月收入
		if(a.getAppIncome() != null && ! a.getAppIncome().toString().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("APP_INCOME",a.getAppIncome().toString() );
		}	
		//近3个月内同类型贷款审批拒绝次数
		if(a.getLoanRefuseCountM3() != null && ! a.getLoanRefuseCountM3().toString().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("LOAN_REFUSE_COUNT_M3",a.getLoanRefuseCountM3().toString() );
		}	
		//是否是例外授信
		if(a.getExceptionCredit() != null && ! a.getExceptionCredit().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("EXCEPTION_CREDIT",a.getExceptionCredit() );
		}	
		//申请人同产品授信总额
		if(a.getAppTotalCa() != null && ! a.getAppTotalCa().toString().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("APP_TOTAL_CA",a.getAppTotalCa().toString() );
		}	
		//申请人行内贷款余额
		if( a.getAppLoanBalance()!= null && ! a.getAppLoanBalance().toString().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("APP_LOAN_BALANCE",a.getAppLoanBalance().toString() );
		}	
		//贷款逾期总期数
		if(a.getLoanOverdueTerm() != null && ! a.getLoanOverdueTerm().toString().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("LOAN_OVERDUE_TERM",a.getLoanOverdueTerm().toString() );
		}	
		//信用卡逾期总期数
		if( a.getCcOverdueTerm()!= null && ! a.getCcOverdueTerm().toString().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("CC_OVERDUE_TERM",a.getCcOverdueTerm().toString() );
		}	
		//房屋价值总和
		if(a.getTotalHouseValue() != null && !a.getTotalHouseValue().toString().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("TOTAL_HOUSE_VALUE",a.getTotalHouseValue().toString() );
		}	
		//还款方式
		if( a.getPaymentMethod()!= null && ! a.getPaymentMethod().trim().equals(""))
		{
			APPLICANT_LOAN_INFO.addAttribute("PAYMENT_METHOD",a.getPaymentMethod() );
		}	
		//贷款最近6个月内的最大逾期期数
		if(a.getLoanOverdueTermMaxM6()!=null && !a.getLoanOverdueTermMaxM6().toString().trim().equals("")){
			APPLICANT_LOAN_INFO.addAttribute("LOAN_OVERDUE_TERM_MAX_M6",a.getLoanOverdueTermMaxM6().toString() );
		}
		//贷款最近12个月内的最大逾期期数
		if(a.getLoanOverdueTermMaxM12()!=null && !a.getLoanOverdueTermMaxM12().toString().trim().equals("")){
			APPLICANT_LOAN_INFO.addAttribute("LOAN_OVERDUE_TERM_MAX_M12",a.getLoanOverdueTermMaxM12().toString() );
		}
		
	}
	//创建APPLICANT_INFO xml
	private static void applicantInfoXml(Element APPLICANT_INFO,ApplicantInfo a)
	{
		//收入偿债比
		if(a.getIncomeDebtRate() != null && !a.getIncomeDebtRate().toString().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("INCOME_DEBT_RATE", a.getIncomeDebtRate().toString());
		}
		
		//客户号
		if(a.getCustNumber() != null && !a.getCustNumber().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("CUST_NUMBER",a.getCustNumber());
		}
		//申请人姓名
		if(a.getAppName() != null && !a.getAppName().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("APP_NAME",a.getAppName());
		}
		//申请人出生日期
		if( a.getAppBirthday()!= null && !a.getAppBirthday().toString().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("APP_BIRTHDAY", a.getAppBirthday().toString());   
		}
		//申请人性别
		if( a.getAppGender() != null && !a.getAppGender().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("APP_GENDER",a.getAppGender());
		}
		//证件类型
		if(a.getIdType() != null && !a.getIdType().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("ID_TYPE", a.getIdType());
		}
		//证件号码
		if(a.getIdNumber() != null && !a.getIdNumber().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("ID_NUMBER",a.getIdNumber());
		}
		//是否本地人
		if(a.getNativeFlag() != null && !a.getNativeFlag().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("NATIVE_FLAG", a.getNativeFlag());
		}
		//个人去年年收入
		if(a.getAnnualIncomeLy() != null && !a.getAnnualIncomeLy().toString().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("ANNUAL_INCOME_LY", a.getAnnualIncomeLy().toString());
		}
		//个人今年年收入
		if(a.getAnnualIncomeTy() != null && !a.getAnnualIncomeTy().toString().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("",a.getAnnualIncomeTy().toString() );
		}
		//住房状况
		if(a.getHouseSituation() != null && !a.getHouseSituation().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("HOUSE_SITUATION",a.getHouseSituation() );
		}
		//家庭总资产
		if(a.getFamilyTotalAssets() != null && !a.getFamilyTotalAssets().toString().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("FAMILY_TOTAL_ASSETS",a.getFamilyTotalAssets().toString() );
		}
		//家庭总负债
		if( a.getFamilyTotalDebt() != null && !a.getFamilyTotalDebt().toString().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("FAMILY_TOTAL_DEBT",a.getFamilyTotalDebt().toString() );
		}
		//家庭月收入总额
		if( a.getFamilyTiMonthly() != null && !a.getFamilyTiMonthly().toString().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("FAMILY_TI_MONTHLY", a.getFamilyTiMonthly().toString());
		}
		//贷款按月总期数
		if( a.getLoanTerm() != null && !a.getLoanTerm().toString().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("LOAN_TERM", a.getLoanTerm().toString());
		}
		//家庭月供总额
		if( a.getFamilyTaMonthly() != null && ! a.getFamilyTaMonthly().toString().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("FAMILY_TA_MONTHLY", a.getFamilyTaMonthly().toString());
		}
		//评级行业类别
		if(a.getRatingIndustryType() != null && !a.getRatingIndustryType().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("RATING_INDUSTRY_TYPE", a.getRatingIndustryType());
		}
		//评级职务职称
		if( a.getRatingDutyTitles() != null && !a.getRatingDutyTitles().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("RATING_DUTY_TITLES", a.getRatingDutyTitles());
		}
		//婚姻状况
		if( a.getMaritalStatus() != null && !a.getMaritalStatus().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("MARITAL_STATUS", a.getMaritalStatus());
		}
		//学历
		if( a.getEducation() != null && !a.getEducation().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("EDUCATION", a.getEducation());
		}
		//申请人是否行内黑名单客户
		if( a.getAppBankBcFlag() != null && !a.getAppBankBcFlag().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("APP_BANK_BC_FLAG", a.getAppBankBcFlag());
		}
		//申请人是否银监会黑名单客户
		if( a.getAppCbrcBcFlag() != null && !a.getAppCbrcBcFlag().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("APP_CBRC_BC_FLAG", a.getAppCbrcBcFlag());
		}
		//单位名称
		if( a.getCompanyName() != null && ! a.getCompanyName().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("COMPANY_NAME", a.getCompanyName());
		}
		//单位电话
		if( a.getCompanyTelephone() != null && !a.getCompanyTelephone().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("COMPANY_TELEPHONE", a.getCompanyTelephone());
		}
		//居住地址
		if( a.getResidentialAddress() != null && ! a.getResidentialAddress().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("RESIDENTIAL_ADDRESS", a.getResidentialAddress());
		}
		//住宅电话
		if( a.getResidentialTelephone() != null && ! a.getResidentialTelephone().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("RESIDENTIAL_TELEPHONE", a.getResidentialTelephone());
		}
		//手机号码
		if( a.getAppMobilePhone() != null && ! a.getAppMobilePhone().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("APP_MOBILE_PHONE", a.getAppMobilePhone());
		}
		//行业类别
		if( a.getIndustryType() != null && !a.getIndustryType().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("INDUSTRY_TYPE", a.getIndustryType());
		}
		//资产负债比
		if( a.getAssetsDebtRate() != null && ! a.getAssetsDebtRate().toString().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("ASSETS_DEBT_RATE", a.getAssetsDebtRate().toString());
		}
		//申请人年龄
		if(a.getAppAge() != null && !a.getAppAge().toString().trim().equals(""))
		{
			APPLICANT_INFO.addAttribute("APP_AGE", a.getAppAge().toString());
		}
	}
	
	//创建APPLICANT_MORTGAGE_INFO xml
	private static void applicantMortgageInfoXml(Element APPLICANT_MORTGAGE_INFO,ApplicantMortgageInfo a)
	{
		//房屋类别
		if(a.getHouseType() != null && !a.getHouseType().trim().equals(""))
		{
			APPLICANT_MORTGAGE_INFO.addAttribute("HOUSE_TYPE",a.getHouseType() );
		}	
		//房屋建筑年代
		if(a.getHouseBuildDate() != null && !a.getHouseBuildDate().toString().trim().equals(""))
		{
			APPLICANT_MORTGAGE_INFO.addAttribute("HOUSE_BUILD_DATE",a.getHouseBuildDate().toString());
		}	
		//房屋价值
		if( a.getHouserValue()!= null && !a.getHouserValue().toString().trim().equals(""))
		{
			APPLICANT_MORTGAGE_INFO.addAttribute("HOUSER_VALUE",a.getHouserValue().toString() );
		}	
		//抵质押率
		if(a.getMortgageRate() != null && !a.getMortgageRate().toString().trim().equals(""))
		{
			APPLICANT_MORTGAGE_INFO.addAttribute("MORTGAGE_RATE",a.getMortgageRate().toString() );
		}	
		//房龄
		if(a.getHouseAge() != null && !a.getHouseAge().toString().trim().equals(""))
		{
			APPLICANT_MORTGAGE_INFO.addAttribute("HOUSE_AGE",a.getHouseAge().toString() );
		}
	}
	
	//组装APPLICANT_PBOC_INFO xml
	private static void applicantPbocInfoXml(Element APPLICANT_PBOC_INFO,ApplicantPbocInfo a)
	{
		//当前贷款状态
		/*if(a.getLoanStatus() != null && !a.getLoanStatus().trim().equals("")){
			APPLICANT_PBOC_INFO.addAttribute("LOAN_STATUS",a.getLoanStatus());
		}*/
		//贷款最近6个月内最大逾期期数
		/*if(a.getLoanOverdueTermM6() != null && !a.getLoanOverdueTermM6().toString().trim().equals("")){
			APPLICANT_PBOC_INFO.addAttribute("LOAN_OVERDUE_TERM_M6",a.getLoanOverdueTermM6().toString());
		}*/
		//贷款最近12个月内最大逾期期数
		/*if(a.getLoanOverdueTermM12() != null && !a.getLoanOverdueTermM12().toString().trim().equals("")){
			APPLICANT_PBOC_INFO.addAttribute("LOAN_OVERDUE_TERM_M12",a.getLoanOverdueTermM12().toString());
		}*/
		//当前贷款逾期的欠款金额
		/*if(a.getApplicantionInfo() != null && !a.getApplicantionInfo().toString().trim().equals("")){
			APPLICANT_PBOC_INFO.addAttribute("LOAN_OVERDRAFT",a.getApplicantionInfo().toString());
		}*/
		//当前信用卡状态
		/*if(a.getCcStatus() != null && !a.getCcStatus().trim().equals("")){
			APPLICANT_PBOC_INFO.addAttribute("CC_STATUS",a.getCcStatus());
		}*/
		//当前信用卡逾期拖欠期数
		/*if(a.getCcOverdueTerm() != null){
			APPLICANT_PBOC_INFO.addAttribute("CC_OVERDUE_TERM",a.getCcOverdueTerm().toString());
		}*/
		//当前信用卡逾期拖欠金额
		/*if(a.getCcOverdraft() != null){
			APPLICANT_PBOC_INFO.addAttribute("CC_OVERDRAFT",a.getCcOverdraft().toString());
		}*/
		//信用卡最近12个月内的最大逾期期数
		/*if(a.getCcOverdueTermM12() != null){
			APPLICANT_PBOC_INFO.addAttribute("CC_OVERDUE_TERM_M12",a.getCcOverdueTermM12().toString());
		}*/
		//信用卡人民币账户使用率
		if(a.getCcAurRmb() != null){
			APPLICANT_PBOC_INFO.addAttribute("CC_AUR_RMB",a.getCcAurRmb().toString());
		}
		//总透支余额
		if(a.getTotalOverdraftBalance() != null){
			APPLICANT_PBOC_INFO.addAttribute("TOTAL_OVERDRAFT_BALANCE",a.getTotalOverdraftBalance().toString());
		}
		//总信用额度
		if(a.getTotalCreditAmount() != null){
			APPLICANT_PBOC_INFO.addAttribute("TOTAL_CREDIT_AMOUNT", a.getTotalCreditAmount().toString());
		}
		//近3个月内征信贷款查询次数
		if(a.getLoanQueryCountM3() != null){
			APPLICANT_PBOC_INFO.addAttribute("LOAN_QUERY_COUNT_M3", a.getLoanQueryCountM3().toString());
		}
		//对外担保信息中风险分类等级
		if(a.getRiskGrade() != null && !a.getRiskGrade().trim().equals("")){
			APPLICANT_PBOC_INFO.addAttribute("RISK_GRADE", a.getRiskGrade());
		}
		if(a.getApplicantPbocCcInfos().size()>0){
			Iterator iter = a.getApplicantPbocCcInfos().iterator();
			while(iter.hasNext()){
				Element APPLICANT_PBOC_CC_INFO = APPLICANT_PBOC_INFO.addElement("APPLICANT_PBOC_CC_INFO");
					ApplicantPbocCcInfo app = (ApplicantPbocCcInfo)iter.next();
					applicantPbocCcInfoXML(APPLICANT_PBOC_CC_INFO,app);
			}
			
		}
		if(a.getApplicantPbocLoanInfos().size()>0){
			Iterator iter = a.getApplicantPbocLoanInfos().iterator();
			while(iter.hasNext()){
				Element APPLICANT_PBOC_LOAN_INFO = APPLICANT_PBOC_INFO.addElement("APPLICANT_PBOC_LOAN_INFO");
					ApplicantPbocLoanInfo app = (ApplicantPbocLoanInfo)iter.next();
					applicantPbocLoanInfoXML(APPLICANT_PBOC_LOAN_INFO,app);
			}
			
		}
	}
	
	public static void applicantPbocCcInfoXML(Element APPLICANT_PBOC_CC_INFO,ApplicantPbocCcInfo app){
		//当前信用卡状态
		if(app.getCcStatus()!=null && !app.getCcStatus().trim().equals("")){
			APPLICANT_PBOC_CC_INFO.addAttribute("CC_STATUS", app.getCcStatus());
		}
		//当前信用卡逾期拖欠期数
		if(app.getCcOverdueTerm()!=null){
			APPLICANT_PBOC_CC_INFO.addAttribute("CC_OVERDUE_TERM", app.getCcOverdueTerm().toString());
		}
		//当前信用卡逾期拖欠金额
		if(app.getCcOverdraft()!=null){
			APPLICANT_PBOC_CC_INFO.addAttribute("CC_OVERDRAFT", app.getCcOverdraft().toString());
		}
	}
	
	public static void applicantPbocLoanInfoXML(Element APPLICANT_PBOC_LOAN_INFO,ApplicantPbocLoanInfo app){
		//当前贷款状态
		if(app.getLoanStatus()!=null && !app.getLoanStatus().trim().equals("")){
			APPLICANT_PBOC_LOAN_INFO.addAttribute("LOAN_STATUS", app.getLoanStatus());
		}
		//当前贷款逾期的欠款金额
		if(app.getLoanOverdraft()!=null){
			APPLICANT_PBOC_LOAN_INFO.addAttribute("LOAN_OVERDRAFT", app.getLoanOverdraft().toString());
		}
	}
	

	/**读取xml文件存入对象
	 * @throws DocumentException */
	public static IN_PreApproval readRiskWarningXml(String xmlVal,IN_PreApproval ip) throws DocumentException
	{
		Document doc = string2Document(xmlVal);
		Element root = doc.getRootElement();
		Element root2 = root.element("APPLICANTION_DECISION_RESULT");
		@SuppressWarnings("unchecked")
		List<Element> root3 = root2.elements("TRIGGER_RULE");
		@SuppressWarnings("unchecked")
		List<Element> root4 = root2.elements("DECISION_FLOW_ITEM_INFO");
		//如果没有触发风险预警,既无决策结果,则返回Null
		if(root2.attributes().size()<=0){
			return null;
		}
		ApplicantionDecisionResult result = new ApplicantionDecisionResult();
		//评分分数
		if(root2.attribute("SCORE")!= null)
		{
			result.setScore(new BigDecimal(root2.attribute("SCORE").getValue()));	
		}
		//业务模块
		if(root2.attribute("BUSINESS_MODULE")!= null) 
		{
			result.setBusinessModule(root2.attribute("BUSINESS_MODULE").getValue());
		}
		//决策结果
		if(root2.attribute("DECISION_RESULT")!= null)
		{
			result.setDecisionResult(root2.attribute("DECISION_RESULT").getValue());
		}
		//风险等级
		if(root2.attribute("RISK_GRADE")!= null)
		{
			result.setRiskGrade(root2.attribute("RISK_GRADE").getValue());
		}
		//初始额度
		if(root2.attribute("INITIAL_AMOUNT")!= null)
		{
			result.setInitialAmount(new BigDecimal(root2.attribute("INITIAL_AMOUNT").getValue()));
		}
		//申请评分分数段系数
		if(root2.attribute("SCORE_GRADE_RATE")!= null)
		{
			result.setScoreGradeRate(new BigDecimal(root2.attribute("SCORE_GRADE_RATE").getValue()));
		}
		//最终贷款额度
		if(root2.attribute("FINAL_LOAN_AMOUNT")!= null)
		{
			result.setFinalLoanAmount(new BigDecimal(root2.attribute("FINAL_LOAN_AMOUNT").getValue()));
		}
		
		//业务模块
		if(root2.attribute("BUSINESS_MODULE")!= null)
		{
			result.setBusinessModule(root2.attribute("BUSINESS_MODULE").getValue());
		}
		
		/**读取TRIGGER_RULE节点数据(节点数量不为一) */
		if(root3.size() > 0)
		{
			for(int i = 0;i < root3.size();i++)
			{
				Element e = root3.get(i);
				TriggerRule p = new TriggerRule();
				
				if(e.attribute("RULESET_CODE") != null)
				{
					p.setRulesetCode(e.attribute("RULESET_CODE").getValue());
				}
				if(e.attribute("RULESET_NAME") != null)
				{
					p.setRulesetName(e.attribute("RULESET_NAME").getValue());
				}
				if(e.attribute("RULE_CODE") != null)
				{
					p.setRuleCode(e.attribute("RULE_CODE").getValue());
				}
				if(e.attribute("RULE_NAME") != null)
				{
					p.setRuleName(e.attribute("RULE_NAME").getValue());
				}
				if(e.attribute("RULE_PRIORITY") != null)
				{
					p.setRulePriority(new BigDecimal(e.attribute("RULE_PRIORITY").getValue()));
				}
				if(e.attribute("RULE_RESULT_DESC") != null)
				{
					p.setRuleResultDesc(e.attribute("RULE_RESULT_DESC").getValue());
				}
				if(e.attribute("RULE_RESULT") != null)
				{
					p.setRuleResult(e.attribute("RULE_RESULT").getValue());
				}
				if(e.attribute("RULE_RISK_GRADE") != null)
				{
					p.setRuleRiskGrade(e.attribute("RULE_RISK_GRADE").getValue());
				}
				
//				List<Element> rootReason = root3.get(i).elements("TRIGGER_RULE");
//				for(int y = 0;y < rootReason.size();i++){
//					Element eReason = rootReason.get(y);
//					Reason reason = new Reason();
//					if(eReason.attribute("REASON_CODE") != null){
//						reason.setReasonCode(eReason.attribute("REASON_CODE").getValue());
//					}
//					if(eReason.attribute("REASON_DETAIL") != null){
//						reason.setReasonDetail(eReason.attribute("REASON_DETAIL").getValue());
//					}
//					if(eReason.attribute("REASON_TYPE") != null){
//						reason.setReasonType(eReason.attribute("REASON_TYPE").getValue());
//					}
//					p.getReasons().add(reason);
//				}
				result.getTriggerRules().add(p);
			}
			
		}
		
		//List<DecisionFlowItemInfo> listdfi = new ArrayList<DecisionFlowItemInfo>();
		if(root4.size() > 0){			
			for(int i = 0;i < root4.size();i++){
				DecisionFlowItemInfo decFlow = new DecisionFlowItemInfo();	
				Element e = root4.get(i);
				if(e.attribute("RULE_FLOW_NAME") != null){
					decFlow.setRuleFlowName(e.attribute("RULE_FLOW_NAME").getValue());
				}
				if(e.attribute("RULE_FLOW_NUMBER") != null){
					BigDecimal b= new BigDecimal(e.attribute("RULE_FLOW_NUMBER").getValue());
					decFlow.setRuleFlowNumber(b);
				}
				
				//List<DecisionComponentItemInfo> listDci = new ArrayList<DecisionComponentItemInfo>();
				Set<DecisionComponentItemInfo> listDci = new HashSet<DecisionComponentItemInfo>();
				for(int temp = 0;temp< e.elements("DECISION_COMPONENT_ITEM_INFO").size();temp++){
					Element eComponent = (Element)e.elements("DECISION_COMPONENT_ITEM_INFO").get(temp);
					DecisionComponentItemInfo dci = new DecisionComponentItemInfo();
					if(eComponent.attribute("COMPONENT_NAME") != null){
						dci.setComponentName(eComponent.attribute("COMPONENT_NAME").getValue());
					}
					if(eComponent.attribute("COMPONENT_NUMBER") != null){
						BigDecimal b = new BigDecimal(eComponent.attribute("COMPONENT_NUMBER").getValue());
						dci.setComponentNumber(b);
					}
					if(eComponent.attribute("COMPONENT_TYPE") != null){
						dci.setComponentType(eComponent.attribute("COMPONENT_TYPE").getValue());
					}
					listDci.add(dci);
					Set<RuleItemInfo> setRules = new HashSet<RuleItemInfo>(); 
					//List<RuleItemInfo> listRule = new ArrayList<RuleItemInfo>();
					for(int intRule = 0;intRule<eComponent.elements("RULE_ITEM_INFO").size();intRule++){
						Element eRuleiteminfo =  (Element)eComponent.elements("RULE_ITEM_INFO").get(intRule);
						RuleItemInfo ruleItemInfo = new RuleItemInfo();
						if(eRuleiteminfo.attribute("RULE_NAME") != null){
							ruleItemInfo.setRuleName(eRuleiteminfo.attribute("RULE_NAME").getValue());
						}
						if(eRuleiteminfo.attribute("RULE_NO") != null){
							BigDecimal b = new BigDecimal(eRuleiteminfo.attribute("RULE_NO").getValue());
							ruleItemInfo.setRuleNo(b);
						}
						setRules.add(ruleItemInfo);
					}
					dci.setRuleItemInfos(setRules);
				}
				decFlow.setDecisionComponentItemInfos(listDci);
				result.getDecisionFlowItemInfos().add(decFlow);
			} 
		}		
		//IN_PreApproval in = new IN_PreApproval();
		ip.setApplicantionDecisionResult(result);
		return ip;
	}
	
}
